#!/usr/bin/perl
#index.cgi

require './tgtman-lib.pl';
&ReadParse();


&header($text{'tgt_title'}, "", undef, 0, 1,);
@tgtlist = &get_target_list();
$line=@tgtlist;
print "<form name=trgt action=index.cgi method=post onsubmit='return onsubmit_form();'>\n";

    print "<table width=100% class=mode2><tr>\n";
    print "<td  valign=top><div class=main><table  class=mode1 border=0 bordercolor=#33ccff cellspacing=0 cellpadding=1 width=100%>\n";
    print "<tr class=title1> <th align=left>$text{'target_number'}</th> ",
          "<th align=left>$text{'target_name'}</th> ",
          "<th align=left>$text{'target_user'}</th>",
	  "<th align=left>$text{'target_device'}</th>",
          "<th align=left>$text{'target_status'}</th> </tr>\n";
    foreach(@tgtlist){
	$tgt_index=$_;
	print "<tr class=mode1>\n";
	print "<td><input type=radio name=targetid value=\"$tgt_index->{'fullname'}\">$tgt_index->{'tid'}</td>\n";
	print "<td nowarp>$tgt_index->{'fullname'}</td>\n";
	print "<td nowarp>$tgt_index->{'user'}</td>\n";
	print "<td nowarp>$tgt_index->{'device'}</td>\n";
	$status= &get_session($tgt_index->{'fullname'});
	if($status eq 'connected'){
		print "<td nowarp>$text{'stat_connected'}</td>\n";
	}
	elsif($status eq 'unconnected'){
		print "<td nowarp>$text{'stat_unconnected'}<td>\n";
	}
	else{
		print "<td nowarp>$text{'stat_stop'}</td>\n";
	}
    }
    print "</table></div></td>\n";
    print "<td valign=top width=160><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
    print "<tr><td align=center><input type=submit name=tgtcrt value=$text{'create'} class=button onclick='return on_crt_click()' disabled=true></td></tr>\n";
    print "<tr><td align=center><input type=submit name=tgtmod value=$text{'modify'} class=button onclick='return on_mod_click($line)'></td></tr>\n";
    print "<tr><td align=center><input type=submit name=ipset value=\"$text{'bt_ipset'}\" class=button onclick='return on_ip_click($line)'></td></tr>\n";
    print "<tr><td align=center><input type=submit name=tgtdel value=$text{'delete'} class=button onclick='return on_del_click($line)'></td></tr>\n";    
    print "</table></td></tr>";
    print "<tr><td colspan=2><i>$text{'note'}</i></td></tr>";
    print "</table>";
print "<input type=hidden name=command>\n";

print "</form>\n";
&footer("/", $text{'index'});


print <<EOF;
<script>
function onsubmit_form()
{
    document.trgt.tgtcrt.disabled = true;
    document.modform.tgtmod.disabled = true;
    document.modform.tgtdel.disabled = true;
    document.modform.mdelete.disabled = true;
}

function on_mod_click(length)
{
    var i;
    var find = 0;
    if(length < 2)
    {
        if(length < 1)
        {
            return false;
        }
        else
        {
            if(document.trgt.targetid.checked)
            {
                 find = 1;
            }
        }
    }
    else
    {
        for(i = 0; i < length; i++)
        {
            if(document.trgt.targetid[i].checked)
            {
                 find = 1;
            }
        }
    }
    if(find == 0)
    {
        alert("$text{'seltarget'}");
        return false;
    }
    document.trgt.action = "target_mod.cgi";
}

function on_ip_click(length)
{
    var i;
    var find = 0;
    if(length < 2)
    {
        if(length < 1)
        {
            return false;
        }
        else
        {
            if(document.trgt.targetid.checked)
            {
                 find = 1;
            }
        }
    }
    else
    {
        for(i = 0; i < length; i++)
        {
            if(document.trgt.targetid[i].checked)
            {
                 find = 1;
            }
        }
    }
    if(find == 0)
    {
        alert("$text{'seltarget'}");
        return false;
    }
    document.trgt.action = "target_iprule.cgi";
}

function on_del_click(length)
{
    var i;
    var find = 0;
    if(length < 2)
    {
        if(length < 1)
        {
            return false;
        }
        else
        {
            if(document.trgt.targetid.checked)
            {
                 find = 1;
            }
        }
    }
    else
    {
        for(i = 0; i < length; i++)
        {
            if(document.trgt.targetid[i].checked)
            {
                 find = 1;
            }
        }
    }
    if(find == 0)
    {
        alert("$text{'seltarget'}");
        return false;
    }
    if(!confirm("$text{'delconfirm'}"))
    {
	return false;
    }
    document.trgt.action = "target_del.cgi";
}

function on_crt_click()
{
    document.trgt.action = 'target_crt.cgi';
    document.modform.command.value = 'modcreate'; 
}

</script>
EOF
